//
//  HDCFMyInfoVc.m
//  HaoDeCaiFu
//
//  Created by 郑开宏 on 2017/11/9.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFMyInfoVc.h"
#import "HDCFSettingCell.h"
#import "SVProgressHUD.h"
#import "HDCFModifyPhoneVc.h"
#import "HDCFModifyNameVC.h"

@interface HDCFMyInfoVc ()<UITableViewDelegate, UITableViewDataSource, HDCFModifyPhoneVcDelegate,HDCFModifyNameVCDelegate>


@property(nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) NSDictionary *personalDict;
@property (nonatomic, strong) NSArray *imageAry;
@property (nonatomic, strong) NSArray *titleArray;

@end

@implementation HDCFMyInfoVc

- (NSArray *)imageAry{
    if (!_imageAry) {
        self.imageAry = [NSArray arrayWithObjects:@"icon_name", @"iocn_shenfenzheng", @"icon_yuliushouji", nil];
    }
    return _imageAry;
}

- (NSArray *)titleArray{
    if (!_titleArray) {
        self.titleArray = [NSArray arrayWithObjects:@"姓名", @"证件号", @"手机号", nil];
    }
    return _titleArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"个人信息"];
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorColor = ZKcolor(245, 245, 245);
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:tableView];
    
    [self getPersonInfo];
}


- (void)getPersonInfo{
    [SVProgressHUD show];
    [HttpRequest sendTwoSelfGetRequestWithURL:@"api/User/GetUser" WithParamets:nil success:^(id result) {
        if ([result[@"code"] isEqual:@1]) {

            [SVProgressHUD dismiss];
            self.personalDict = result[@"result"];
//            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//            [defaults setValue:self.personalDict forKey:@"mineInfo"];
//            [defaults synchronize];
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showInfoWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellid";
    HDCFSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [HDCFSettingCell HDCFSettingCell];
    }
    NSString *imgName= self.imageAry[indexPath.row];
    cell.imgView.image = [UIImage imageNamed:imgName];
    
    NSString *title = self.titleArray[indexPath.row];
    cell.titleLable.text = title;
    if (self.personalDict) {
        if (indexPath.row == 0) {
            if ([self.personalDict[@"Name"] isKindOfClass:[NSNull class]]) {
                cell.detailLabel.text = @"";
            }else{
                cell.detailLabel.text = self.personalDict[@"Name"];
            }
            
        }else if (indexPath.row == 1) {
            if ([self.personalDict[@"IdNumber"] isKindOfClass:[NSNull class]]) {
                cell.detailLabel.text = @"";
            }else{
                NSString *idNo = self.personalDict[@"IdNumber"];
                cell.detailLabel.text = [NSString stringWithFormat:@"**************%@", [idNo substringFromIndex:idNo.length - 4]];
            }
            
        }else{
            cell.detailLabel.text = self.personalDict[@"Phone"];
        }
    }else{
        if (indexPath.row == 0) {
            cell.detailLabel.text = @"";
        }else if (indexPath.row == 1) {
            cell.detailLabel.text = @"";
        }else{
            cell.detailLabel.text = @"";
        }
        
    }
    
    
    if (indexPath.row == 2 || indexPath.row == 0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        HDCFModifyNameVC * vc = [[HDCFModifyNameVC alloc]init];
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.row == 2) {
        HDCFModifyPhoneVc *vc = [[HDCFModifyPhoneVc alloc] init];
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark -
#pragma mark HDCFModifyNameVCDelegate
-(void)returnHDCFModifyName:(NSString*)name
{
    [self getPersonInfo];
}
- (void)modifyPhoneSucess{
    [self getPersonInfo];
}

@end
